﻿<MudItem xs="12">
    <MudSelect T="string" Value="EditData.DataSourceTypeSelect"
               ValueChanged="DataSourceTypeChangedHandle"
               For="@(() => EditData.DataSourceTypeSelect)" Label="数据源类型"
               Dense="true" AnchorOrigin="Origin.BottomCenter">
        <MudSelectItem Value="@("database")">自定义Sql</MudSelectItem>
        <MudSelectItem Value="@("valuePair")">自定义键值对</MudSelectItem>
    </MudSelect>
</MudItem>

@if (dataSourceType.Equals("database"))
{
    <MudItem xs="12">
        <MudField Label="SQL语句" Variant="Variant.Text" InnerPadding="false">
            <StandaloneCodeEditor Id="my-editor-instance-id" ConstructionOptions="EditorConstructionOptions" CssClass="my-editor-class" />
        </MudField>
    </MudItem>

    <MudItem xs="6">
        <MudSelect T="string" @bind-Value="EditData.DataSourceValueSelect"
               For="@(()=>EditData.DataSourceValueSelect)" Label="键字段"
               Dense="true" AnchorOrigin="Origin.BottomCenter">
            @*@foreach (var item in _dataTableCols)
        {
        <MudSelectItem Value="@(item.ColumnName)">@item.ColumnName</MudSelectItem>
        }*@
        </MudSelect>
    </MudItem>
    <MudItem xs="6">
        <MudSelect T="string" @bind-Value="EditData.DataSourceTextSelect" For="@(()=>EditData.DataSourceTextSelect)"
               Label="值字段" Dense="true" AnchorOrigin="Origin.BottomCenter">
            @*@foreach (var item in _dataTableCols)
        {
        <MudSelectItem Value="@(item.ColumnName)">@item.ColumnName</MudSelectItem>
        }*@
        </MudSelect>
    </MudItem>
}
else
{
    @foreach (var item in EditData.DataSourceStaticValueSelect)
    {
        <MudItem xs="5">
            <MudTextField @bind-Value="item.Key"
                  For="@(()=>item.Key)" Label="键字段"></MudTextField>
        </MudItem>
        <MudItem xs="5">
            <MudTextField @bind-Value="item.Value"
                  For="@(()=>item.Value)" Label="值字段"></MudTextField>
        </MudItem>
        <MudItem xs="2">
            <MudIconButton Color="Color.Error" Icon="@Icons.Material.Filled.Remove"
                   OnClick="@(()=>RemoveItemClickHandle(item))"></MudIconButton>
        </MudItem>
    }

    <MudButton StartIcon="@Icons.Material.Filled.Add" IconColor="Color.Primary"
           OnClick="AddItemClickHandle">添加字段</MudButton>
}


@code {
    [CascadingParameter]
    [NotNull]
    public ConfigFieldEditRequestDto? EditData { get; set; }

    private string dataSourceType = "database";

    private StandaloneEditorConstructionOptions EditorConstructionOptions(StandaloneCodeEditor editor)
    {
        return new StandaloneEditorConstructionOptions
            {
                Language = "sql",
                GlyphMargin = true,
                Value = "",
                AutomaticLayout = true
            };
    }

    Task DataSourceTypeChangedHandle(string value)
    {
        if (!string.IsNullOrEmpty(value))
        {
            EditData.DataSourceTypeSelect = value;
            dataSourceType = value;
        }
        return Task.CompletedTask;
    }

    public void AddItemClickHandle()
    {
        if (EditData.DataSourceStaticValueSelect.Any(i => string.IsNullOrEmpty(i.Key)))
        {
            return;
        }

        EditData.DataSourceStaticValueSelect
                .Add(new ConfigStaticValueModelDto());
    }

    public void RemoveItemClickHandle(ConfigStaticValueModelDto item)
    {
        EditData.DataSourceStaticValueSelect.Remove(item);
    }

    protected override void OnInitialized()
    {
        if (EditData != null && !string.IsNullOrEmpty(EditData.DataSourceTypeSelect))
            dataSourceType = EditData.DataSourceTypeSelect;
        base.OnInitialized();
    }
}